layui.config({
    base: ctx + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index',         //主入口模块
    jsonviewer: 'jsonviewer'
}).use(['index', 'table', 'jsonviewer', 'layedit', 'code', 'util'], function () {
    var prefix = ctx + "/manage/reptile/answerReptile";
    var $ = layui.$, form = layui.form, layedit = layui.layedit;

    //监听爬取数据类型
    form.on('radio(type)', function (data) {
        var type = data.value;
        if (type == '1') {
            $('.page').show();
        } else {
            $('.page').hide();
        }
    });

    //监听展示方式
    form.on('switch(showSwitch)', function (data) {
        if (data.elem.checked == true) {
            $('#JsonDiv').show();
            $('#LayEditDiv').hide();
        } else {
            $('#JsonDiv').hide();
            $('#LayEditDiv').show();
        }
    });

    //监听保存
    form.on('submit(LAY-save)', function (data) {
        var paperId = data.field.paperId || '';
        if (paperId == '') {
            commonJs.openMsg('cry', '更新试题的id不能为空！');
            return;
        }
        commonJs.openConfirm('确定将该爬取链接的结果更新到数据库表吗？', function () {
            commonJs.openAjax(prefix + "/save", data, function (result) {
                if (result.code == 0) {
                    commonJs.openMsg('success', '更新成功！');
                } else {
                    commonJs.openMsg('error', result.msg);
                }
            });
        });
    });

    //监听搜索
    form.on('submit(LAY-search)', function (data) {
        var field = data.field;
        var type = field.type;
        commonJs.openAjax(prefix + "/reptile", field, function (result) {
            if (result.code == 0) {
                commonJs.openMsg('success', '数据爬取成功！', function () {
                    $('#JsonDiv').html('');
                    $('#LayEditDiv').html('');
                    $('#showTypeDiv').show();

                    //展示Json格式
                    $('#JsonDiv').append(createJsonHtml());
                    $('#JsonResult').jsonViewer(result, {});
                    layui.code({
                        elem: 'pre',
                        title: 'JSON格式数据',
                        about: false
                    });

                    //展示富文本
                    var list = result.data || [];
                    $('#LayEditDiv').append('<label class="layui-form-label">爬取结果</label>');
                    $.each(list, function (index, item) {
                        $('#LayEditDiv').append(createLayEditHtml(index, item));
                        layedit.build('LayEditResult' + index);
                    });
                    if (type == '1') {
                        $('#JsonDiv').show();
                        $('#LayEditDiv').hide();
                        $("#showType").attr('checked', 'true');
                        form.render("checkbox");
                    } else {
                        $('#JsonDiv').hide();
                        $('#LayEditDiv').show();
                    }
                });
            } else {
                commonJs.openMsg('error', result.msg, function () {
                    $('#JsonDiv').hide();
                    $('#LayEditDiv').hide();
                    $('#showTypeDiv').hide();
                });
            }
        });
    });

    //生成JSON的html
    function createJsonHtml() {
        var contentHtml = '<label class="layui-form-label">爬取结果</label>';
        contentHtml += '<div class="layui-input-block">';
        contentHtml += '<pre id="JsonResult" class="layui-code"></pre>';
        contentHtml += '</div>';
        return contentHtml;
    }

    //生成富文本的html
    function createLayEditHtml(index, item) {
        item = item || '暂无数据';
        if (Object.prototype.toString.call(item) === '[object Object]') {
            item = JSON.stringify(item);
        }
        var contentHtml = '<div class="layui-input-block" style="margin-bottom:20px;">';
        contentHtml += '<textarea id="LayEditResult' + index + '">' + item + '</textarea>';
        contentHtml += '</div>';
        return contentHtml;
    }

    //回到顶部按钮
    util.fixbar({
        bgcolor: '#3B91FF',
        showHeight: 100,
        click: function (type) {
            if (type === 'top') {
                $('.layui-body').animate({
                    scrollTop: 0
                }, 200);
            }
        }
    });
});